package com.mall.common.validation;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.mall.common.bean.WebErrors;
import com.mall.common.plugin.AutoRegisterPlugin;
import com.mall.common.validation.annotation.RequiredFieldValidator;
import com.mall.common.validation.annotation.Validations;

public class RequiredFieldValidate extends AutoRegisterPlugin implements IValidate {

	public WebErrors validate(HttpServletRequest request, WebErrors errors, 
			Validations validations, Map<String, String> decodedUriVariables){
		RequiredFieldValidator[] rFiled = validations.requiredFields();
		for(RequiredFieldValidator rfv : rFiled){
			String fieldName = rfv.fieldName();
			String value = request.getParameter(fieldName);
			if(value == null && decodedUriVariables != null){
				value = decodedUriVariables.get(fieldName);
			}
			if(value == null){
				errors.addErrorString(rfv.message());
			}
		}
		return errors;
	}

	@Override
	public String getType() {
		return "vallidate";
	}

	@Override
	public String getId() {
		return "requiredFiledValidate";
	}

	@Override
	public String getName() {
		return "对象非空校验";
	}

	@Override
	public String getVersion() {
		return "1.0.0";
	}

	@Override
	public String getAuthor() {
		return "fangchen";
	}

	@Override
	public void perform(Object... params) {
		
	}

	@Override
	public void register() {
		
	}
}
